<?php
namespace Common\Model;
use Think\Model;
/**
 * 二维码表
 */
class WechatQrcodeModel extends Model{

    protected $_auto = array(/* array(完成字段1,完成规则,[完成条件,附加规则]) */
        array('addTime', 'time', 1, 'function'),
        array('updateTime', 'time', 2, 'function'),
        array('isOn', '1', 1),
    );  // 自动完成定义

    /**
     *  获取单个信息
     */
    public function getQrcode($id = null, $where = array()) {
        if (!empty($id)) {
            $where['qrID'] = $id;
        }

        $result = $this->where($where)->find();

        return $result;
    }
    
    /**
     *  二维码增加一个入口统计
     */
    public function qrcodeIncCount($scenceID){
    	$where=array(
    		"scenceID"=>$scenceID
    	);
    	$result=$this->where($where)->setInc("count",1);
    	return $result;
    }

    /**
     * 获得列表（带基本查询）
     */
    public function getQrcodeList($page = 1, $pageSize = 15, $where = array(), $order = array()) {
        $result = $this->where($where)->page($page, $pageSize)->order($order)->select();
           return $result;
    }

    public function getQrcodeListLength($where = array()) {
        return $this->where($where)->count();
    }

    /**
     * 添加单个信息
     * @return type
     */
    public function addQrcode($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->add();
        }
        return $rt;
    }

    /**
     * 更新信息(注意$data一定要有主键id,若$data为空，则默认是对$_POST数组处理)
     * @return type
     */
    public function updateQrcode($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->save();
        }
        return $rt;
    }

    /**
     * 删除
     */
    public function deleteQrcode($id) {
        if ($id <= 0) {
            $this->error = "无效ID";
            return false;
        }
        $data = array("isOn" => 0, "updateTime" => time());
        return $this->where("qrID=$id")->save($data);
    }
    
    /**
     * 获取一个任务
     */
    public function getQrcodeTask($taskID){
    	$where=array(
    		"taskID"=>$taskID,
    		"isOn"=>1
    	);
    	$result=M("WechatQrcodeTask")->where($where)->find();
    	return $result;
    }
	
    /**
     * 获取多个未使用二维码id
     */
    public function getNoUsedMutiQrcodeID($num){
    	$where=array(
    		"status"=>2,
    	);
        
    	return $this->where($where)->limit(intval($num))->getField("qrID",true);
    }
    
}
?>